/******************************************************************************

    Copyright (C) 2006 Alessio Stalla (alessiostalla@gmail.com)

    This file is part of PrettyProlog.

    PrettyProlog is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    PrettyProlog is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PrettyProlog; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    See the file COPYING for further details.

 *****************************************************************************/

package prettyprolog.datatypes;

import java.util.Iterator;

/**
 * A Cons is a pair of Terms, the car and the cdr. Conses are used to build
 * lists, and indeed some methods of this class treat a Cons like a list.
 */
public class Cons implements Term, Iterable<Term> {

	private static final long serialVersionUID = 1L;
	
	protected Term car;
	protected Term cdr;

	/** Constructs a new Cons with the given car and cdr. */
	public Cons(Term car, Term cdr) {
		setCar(car);
		setCdr(cdr);
	}

	/** Constructs a new Cons with the given car and a null cdr. */
	public Cons(Term car) {
		this(car, Nil.NIL);
	}
	
	protected Cons() {}

	/** @return the car. */
	public Term car() {
		return car;
	}

	/** Sets the car of this Cons. */
	public void setCar(Term car) {
		if(car == null) {
			throw new NullPointerException("car must not be null");
		}
		this.car = car;
	}

	/** @return the cdr. */
	public Term cdr() {
		return cdr;
	}

	/**
	 * Sets the cdr.
	 */
	public void setCdr(Term cdr) {
		if(cdr == null) {
			throw new NullPointerException("cdr must not be null");
		}
		this.cdr = cdr;
	}

	/**
	 * @return true if t is contained in the list starting with this Cons, false
	 *         otherwise.
	 */
	public boolean contains(Term t) {
		return contains(t, false);
	}

	/**
	 * @return true if t is contained in the list starting with this Cons or, if
	 *         <code>deep == true</code>, in any of this list's elements, false
	 *         otherwise.
	 */
	public boolean contains(Term t, boolean deep) {
		if (car.equals(t) || (deep && car.contains(t, deep))) {
			return true;
		}
		if(cdr != Nil.NIL) {
			if (cdr instanceof Cons) {
				return ((Cons) cdr).contains(t, deep);
			} else {
				return cdr.equals(t) || (deep && cdr.contains(t, deep));
			}
		}
		return false;
	}

	public Term apply(Substitution subst) {
		return new Cons(car.apply(subst), cdr.apply(subst));
	}

	public boolean isGround() {
		return isGround(true);
	}

	public boolean isGround(boolean strict) {
		return (car.isGround(strict) && cdr.isGround(strict));
	}

	public Term copy(Substitution vars) {
		Cons copy = new Cons();
		copy.setCar(car().copy(vars));
		copy.setCdr(cdr().copy(vars));
		return copy;
	}

	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		} else if (obj instanceof Cons) {
			Cons cobj = (Cons) obj;
			return car.equals(cobj.car) && cdr.equals(cobj.cdr);
		}
		return false;
	}

	public int hashCode() {
		return car.hashCode() + cdr.hashCode();
	}

	private String toString(boolean parens) {
		String str = "";
		if (parens) {
			str += "[";
		}
		str += car;
		if (cdr != Nil.NIL) {
			if (cdr instanceof Cons) {
				str += ", " + ((Cons) cdr).toString(false);
			} else {
				str += "|" + cdr;
			}
		}
		if (parens) {
			str += "]";
		}
		return str;
	}

	public String toString() {
		return toString(true);
	}

	public int length() {
		int l = 1;
		Term t = cdr();
		while(t != Nil.NIL && t instanceof Cons) {
			l++;
			t = ((Cons) t).cdr();
		}
		return l;
	}

	public Iterator<Term> iterator() {
		return new Iterator<Term>() {

			protected Cons ptr = Cons.this;
			
			public boolean hasNext() {
				return ptr instanceof Cons;
			}


			public Term next() {
				Term next = ptr.car();
				Term cdr = ptr.cdr();
				if(cdr instanceof Cons) {
					ptr = (Cons) cdr;
				} else {
					ptr = null;
				}
				return next;
			}

			public void remove() {
				throw new UnsupportedOperationException("remove not supported by Cons");
			}
		};
	}
}